package com.ubs.web.client.panel.login;

import com.google.gwt.user.client.ui.VerticalPanel;
import com.ubs.dom.Account;
import com.ubs.web.client.security.SecurityContext;
import com.ubs.web.client.security.SecurityListener;

/**
 * @author box
 * 
 */
public final class LoginPanel extends VerticalPanel implements SecurityListener {

	private static final LoginInputsGrid inputControlsGrid = LoginInputsGrid.getSingleton();
	private static final LoginInfoGrid userInfoGrid = LoginInfoGrid.getSingleton();

	private static LoginPanel panel = null;

	public static LoginPanel getSingleton() {
		if (panel == null) {
			panel = new LoginPanel();
		}
		return panel;
	}

	private LoginPanel() {
		super();
		setSize("200px", "73px");

		Account account = SecurityContext.getAccount();
		userInfoGrid.updateUserInfo(account);

		add(account == null ? inputControlsGrid : userInfoGrid);
	}

	@Override
	public void onAccountLoggedIn(Account account) {
		showUserInfo(account);
	}

	@Override
	public void onAccountLoggedOut() {
		showInputControls();
	}

	@Override
	public void onAccountRefistered(Account account) {
	}

	private void showInputControls() {
		if (getWidget(0) != inputControlsGrid) {
			remove(0);
			add(inputControlsGrid);
		}
	}

	private void showUserInfo(Account account) {
		if (getWidget(0) != userInfoGrid) {
			remove(0);
			userInfoGrid.updateUserInfo(account);
			add(userInfoGrid);
		}
	}

}
